home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mlib44d.zoo / src / getgroup.c
C/C++ Source or Header  |  1994-03-01  |  2KB  |  130 lines

  1. /*
  2.  * FILE
  3.  *    getgroup.c
  4.  *
  5.  * PURPOSE
  6.  *    get the groups the current user is in
  7.  *
  8.  * AUTHORS
  9.  *    written by Ole Arndt and placed in the public domain :-)
  10.  *
  11.  * BUGS
  12.  *    under BSD user has access to all groups he is in at the same time
  13.  *    under MiNT there is only one valid group at a time.
  14.  *  so for suid programs this function gives you the groups you can
  15.  *    switch to for your real user.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <types.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <grp.h>
  24. #include <pwd.h>
  25. #ifdef TEST
  26. #include <stdlib.h> /* for calloc() */
  27. #endif
  28.  
  29. extern int __mint;
  30.  
  31. int
  32. getgroups(gsetlen, grpset)
  33.   int     gsetlen;
  34.   gid_t   *grpset;
  35. {
  36.   struct group    *gentry;
  37.   int             numgroups;
  38.   struct passwd   *userpw;
  39.   int             i;
  40.   gid_t           currgid;
  41.  
  42.   if (!__mint)
  43.     return 0;
  44.  
  45.   currgid = getgid();
  46.   if (gsetlen) {
  47.     if (gsetlen < 0 || !grpset) {
  48.       errno = EINVAL;
  49.       return -1;
  50.     }
  51.     *grpset++ = currgid;
  52.   }
  53.  
  54.   if (geteuid() && getegid())
  55.     return 1;
  56.  
  57.   userpw = getpwuid(getuid());
  58.   if (!userpw) {
  59.     return -1;
  60.   }
  61.  
  62.   numgroups = 1;
  63.   setgrent();
  64.   while ((gentry = getgrent()) != NULL) {
  65.     for (i = 0; gentry->gr_mem[i]; i++) {
  66.       if (!strcmp(userpw->pw_name, gentry->gr_mem[i])
  67.           && (gentry->gr_gid != currgid)) {
  68.         ++numgroups;
  69.         if (gsetlen) {
  70.           if (numgroups > gsetlen) {
  71.             errno = EINVAL;
  72.             return -1;
  73.       }
  74.           *grpset++ = gentry->gr_gid;
  75.           break;
  76.         }
  77.       }
  78.     }
  79.   }
  80.   endgrent();
  81.   return numgroups;
  82. }
  83.  
  84. #ifdef TEST
  85.  
  86. int
  87. main(void)
  88. {
  89.   int ngroups;
  90.   int num, i;
  91.   gid_t *grps;
  92.   struct passwd *userpw;
  93.  
  94.   ngroups = getgroups(0, (gid_t *) NULL);
  95.   if (ngroups < 0) {
  96.     perror("getgroups() first call failed");
  97.     exit(2);
  98.   }
  99.   printf("first call to getgroups(): %d\n", ngroups);
  100.   grps = (gid_t *) calloc(ngroups, sizeof(gid_t));
  101.   if (!grps) {
  102.     perror("calloc() failed");
  103.     exit(2);
  104.   }
  105.   num = getgroups(ngroups, grps);
  106.   if (num < 0) {
  107.     perror("getgroups() second call failed");
  108.     exit(2);
  109.   }
  110.   printf("second call to getgroups(): %d\n", num);
  111.   if (ngroups != num) {
  112.     printf("getgroups() results do not match\n");
  113.     exit(2);
  114.   }
  115.  
  116.   userpw = getpwuid(getuid());
  117.   printf("User %s is in the following groups: ", userpw->pw_name);
  118.  
  119.   if (num == 0)
  120.     printf("none");
  121.   else
  122.     for( i = 0; i < num; i++)
  123.       printf("%d ", grps[i]);
  124.   printf(".\n");
  125.  
  126.   return 0;
  127. }
  128.  
  129. #endif
  130.